﻿using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class BlockManage : BaseUI.BasePage
    {
        private Lc_BlockBLL lbb = new Lc_BlockBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "N_BLOCK_ID";
                ViewState["OrderDire"] = "asc";
                //BindGridView();
                BindData(true);
            }
        }

        //public void BindGridView()
        //{
        //    DataTable dt = lbb.GetAllInfo("", "");
        //    grvShow.DataSource = dt;
        //    grvShow.DataBind();
        //}

        #region 按钮事件

        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditBlock.aspx");
        }

        /// <summary>
        /// 编辑模块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEditModule_Click(object sender, EventArgs e)
        {
            int count = 0;
            string pid = "";
            for (int i = 0; i < this.grvShow.Rows.Count; i++)
            {
                if (((CheckBox)this.grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    pid = ((CheckBox)this.grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                }
            }
            if (count == 1)
            {
                //FORM_EDIT
                MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
            }
            else
            {
                MessageBox.Show(this, "【提示】请选择一项！！！");
                //lblMes.Text = "【提示】请选择一项！！！";
                return;
            }
        }

        /// <summary>
        /// 搜索事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnRemoveClick(object sender, EventArgs e)
        {
            var ids = "";
            for (int i = 0; i < this.grvShow.Rows.Count; i++)
            {
                if (((CheckBox)this.grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    ids += ((CheckBox)this.grvShow.Rows[i].FindControl("chkSelectId")).ToolTip + ",";
                }
            }
            if (ids.Length > 0)
            {
                ids = ids.Substring(0, ids.Length - 1);
            }
            else
            {
                MessageBox.Show(this, "【温馨提示】请至少选择一项！");
                return;
            }
            if (lbb.DELETE(ids, CurrentUserName) > 0)
            {
                BindData(true);
                //BindGridView();
                MessageBox.Show(this, "【温馨提示】删除成功！");
            }
            else
            {
                MessageBox.Show(this, "【温馨提示】删除失败！");
            }
        }

        #endregion 按钮事件

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void BindData(bool isFirst)
        {
            var Type = "";
            string key = this.Search.Text;
            if (String.IsNullOrEmpty(key))
            {
                Type = "";
            }
            else
            {
                try
                {
                    Type = "Key";
                }
                catch (Exception)
                {
                    MessageBox.Show(this, "查询出现异常catch");
                }
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = lbb.GetRecordCount(Type, key);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = lbb.GetListByPage(Type, key, sort, PageIng.CurrentPage, PageIng.RowsPerPage);
            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindData(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        //public void BindProjectName(bool iSsort)
        //{
        //    DataTable dt = null;
        //    string key = this.Search.Text;
        //    if (String.IsNullOrEmpty(key))
        //    {
        //        dt = lbb.GetAllInfo("", "");
        //    }
        //    else
        //    {
        //        dt = lbb.GetAllInfo("Key", key);
        //    }

        //    DataView view = dt.DefaultView;
        //    if (iSsort)
        //    {
        //        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        //        view.Sort = sort;//设置排序
        //    }

        //    grvShow.DataSource = view;

        //    grvShow.DataBind();
        //}
    }
}